﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using MouseExtender.Logic.Entities;
using MouseExtender.Logic.Enums;
using MouseExtender.Logic.Managers;

namespace MouseExtender.Controls.UserControls
{
    /// <summary>
    /// Interaction logic for TabUC.xaml
    /// </summary>
    public partial class TabUC : UserControl
    {
        #region fields

        private TabInfo _currentTabInfo = null;

        #endregion

        #region dependency properties

        public static DependencyProperty LeftPositionProperty = DependencyProperty.Register("LeftPosition", typeof(double), typeof(TabUC));

        #endregion

        #region properties

        /// <summary>
        /// Current tab identity
        /// </summary>
        public int TabId
        {
            get { return TabInfo == null ? -1 : TabInfo.Id; }
        }

        /// <summary>
        /// Current tab metadata
        /// </summary>
        public TabInfo TabInfo
        {
            get
            {
                return _currentTabInfo;
            }
            set
            {
                _currentTabInfo = value;
                RefreshTabItems();
            }
        }

        /// <summary>
        /// Items enumerator source
        /// </summary>
        public IEnumerable<LauncherItem> TabItemsSource
        {
            get
            {
                return SettingsManager.CurrentSettings.LauncherItems.Where(li => this.TabId == -1 || li.TabId == this.TabId);
            }
        }

        /// <summary>
        /// Canvas left position
        /// </summary>
        public double LeftPosition
        {
            get
            {
                return (double)this.GetValue(Canvas.LeftProperty);
            }
            set
            {
                this.SetValue(Canvas.LeftProperty, value);
            }
        }

        private void RefreshTabItems()
        {
            ChildrenContainer.Clear();

            bool isNeedSaveSettings = false;
            LauncherUC launcherRoot = App.MainWindowInstance.ucLauncher;

            foreach (LauncherItem item in TabItemsSource)
            {
                LauncherItemUC launcherItemUC = launcherRoot.GetLauncherItemUC(item, ref isNeedSaveSettings);
                ChildrenContainer.Add(launcherItemUC);
            }

            if (isNeedSaveSettings)
                SettingsManager.CurrentSettings.SettingChanged();

            UpdateTabSize();
            UpdateVisualState();
            UpdateLayout();
        }

        private void UpdateVisualState()
        {
            App.MainWindowInstance.firstTimeNote.Visibility =
                    SettingsManager.CurrentSettings.LauncherItems.Count == 0 || this.TabItemsSource.Count() == 0 ? Visibility.Visible : Visibility.Collapsed;
        }



        /// <summary>
        /// Reference to the children control collection
        /// </summary>
        public UIElementCollection ChildrenContainer
        {
            get { return this.wpPlaceholder.Children; }
        }

        #endregion properties

        public TabUC()
        {
            InitializeComponent();
        }

        private void UpdateTabSize()
        {
            this.Width = CalculateControlWidth();
            //height calculated automatically
        }

        private double CalculateControlWidth()
        {
            double width = Double.NaN;
            if (SettingsManager.CurrentSettings.IconSize == IconSize.Icon32x32)
                width = SettingsManager.CurrentSettings.ItemsInRow * 50;
            else
                width = SettingsManager.CurrentSettings.ItemsInRow * 30;

            return width;
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            //reraise property changes for new position property
            if (e.Property == Canvas.LeftProperty)
            {
                //System.Diagnostics.Trace.WriteLine(string.Format("Canvas.Left of [{0}] changed from [{1}] to [{2}]", this.Name, e.OldValue, e.NewValue));
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(LeftPositionProperty, e.OldValue, e.NewValue));
            }

            if (e.Property == UIElement.VisibilityProperty)
            {
                if ((Visibility)e.NewValue == Visibility.Visible)
                    UpdateVisualState();
            }
        }

        //private double CalculateControlHeight()
        //{
        //    double height = Double.NaN;
        //    int rowCount = (int)Math.Ceiling(TabItemsSource.Count() / (double)SettingsManager.CurrentSettings.ItemsInRow);
        //    if (SettingsManager.CurrentSettings.IconSize == IconSize.Icon32x32)
        //        height = rowCount * 50;
        //    else
        //        height = rowCount * 30;

        //    return height + 10;
        //}
    }
}
